// src/main/java/com/bookmanager/controller/UserRolesController.java
package com.bookmanager.controller;

import com.bookmanager.model.User;
import com.bookmanager.service.UserService;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet("/admin/user-roles")
public class UserRolesController extends HttpServlet {
    private UserService userService = new UserService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");

        // 检查管理员权限
        if (user == null || !"admin".equals(user.getRole())) {
            response.sendRedirect(request.getContextPath() + "/login.jsp");
            return;
        }

        // 获取所有用户数据
        List<User> users = userService.getAllUsers();
        System.out.println("🔍 UserRolesController 获取到用户数量: " + (users != null ? users.size() : 0));

        if (users != null) {
            for (User u : users) {
                System.out.println("用户: " + u.getUserId() + " - " + u.getUsername() + " - " + u.getRole());
            }
        }

        request.setAttribute("users", users);
        request.getRequestDispatcher("/admin/user-roles.jsp").forward(request, response);
    }
}